package com.kcs.fxmargin.service;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.kcs.common.utils.ConnectionUtils;
import com.kcs.common.utils.Constant;
import com.kcs.common.utils.commonUtils;
import com.kcs.fxmargin.dao.DealsSumDao;
import com.kcs.fxmargin.model.TDealsSum;

public class DealsSumService {

	DealsSumDao dao = new DealsSumDao();
	
	public TDealsSum search(String cifNo, String asOfDate, boolean cashFlag) throws Exception {
		
		Connection conn 		= null;
		PreparedStatement pstm 	= null;
		ResultSet rs			= null;
		TDealsSum dealsSum		= new TDealsSum();
		
		try{
			conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO); // get connection fxmargin from mssql
			dealsSum = dao.search(conn, pstm, rs, cifNo);
			
			if(cashFlag == true){
				BigDecimal cashBalance 	= dao.getCashBalance(conn, pstm, rs, cifNo, commonUtils.stringToDate(asOfDate, "dd/MM/yyyy"));
				
				if(dealsSum.getCashBal() != null && cashBalance != null)
					dealsSum.setCashBal(dealsSum.getCashBal().add(cashBalance));
			}
			
		} catch (Exception e){
			e.printStackTrace();
		} finally {
			if(conn != null ) conn.close();
			if(pstm != null ) pstm.close();
			if(rs   != null ) rs.close();
		}
		
		return dealsSum;
	}
}
